<?php

namespace app\store\controller\movies;

use app\store\controller\Controller;
use app\store\controller\Upload;
use app\store\model\Video as VideoModel;
use think\Db;

/**
 * 门店管理
 * Class Shop
 * @package app\store\controller\store
 */
class Video extends Controller
{
    /**
     * 视频列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new VideoModel;
        $list = $model->getList(array_merge(['status' => -1], $this->request->param()));
        foreach ($list as &$value) {
            $value['grade'] = $value['grade_id'] ? Db::name('user_grade')->where('grade_id', $value['grade_id'])->where('wxapp_id', $this->getWxappId())->value('name') : '无限制';
        }
        //var_dump($list);die();
        $catgory = Db::name('video_category')->where('wxapp_id', $this->getWxappId())->select()->toArray();
        return $this->fetch('index', compact('list', 'catgory'));
    }

    /**
     * 添加视频
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {
        if (!$this->request->isAjax()) {
            $catgory = Db::name('video_category')->where('wxapp_id', $this->getWxappId())->select()->toArray();
            $grade = Db::name('user_grade')->where('wxapp_id', $this->getWxappId())->select()->toArray();
            return $this->fetch('add', compact('catgory', 'grade'));
        }
        $data = $this->postData('video');
        $model = new VideoModel;
        if ($model->add($data)) {
            return $this->renderSuccess('添加成功', url('movies.video/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑
     * @param $video_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     */
    public function edit($video_id)
    {
        // 详情
        $model = VideoModel::detail($video_id);
        $catgory = Db::name('video_category')->where('wxapp_id', $this->getWxappId())->select()->toArray();
        $grade = Db::name('user_grade')->where('wxapp_id', $this->getWxappId())->select()->toArray();
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model', 'catgory', 'grade'));
        }
        // 新增记录
        $data = $this->postData('video');
        if ($model->edit($data)) {
            return $this->renderSuccess('更新成功', url('movies.video/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 上传页
     * @param $video_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     */
    public function uploadVideo($video_id = 0)
    {
        $grade = Db::name('user_grade')->where('wxapp_id', $this->getWxappId())->select()->toArray();
        if (!$this->request->isAjax()) {
            return $this->fetch('uploadVideo', compact('grade'));
        }
        // 新增记录
        $data = $this->postData('video');
        $count = Db::name('video_gather')->where('video_id', $video_id)->where('wxapp_id', $this->getWxappId())->count();
        $data['gather_no'] = $count + 1;
        if ($video_id <= 0 || strlen($data['video_url']) <= 0) {
            return $this->renderError('视频id/地址有误');
        }
        Db::startTrans();
        try {
            Db::name('video_gather')->insert([
                'video_id' => $video_id,
                'video_url' => $data['video_url'],
                'grade_id' => $data['grade_id'],
                'gather_no' => $data['gather_no'],
                'wxapp_id' => $this->getWxappId(),
                'create_time' => time()
            ]);
            Db::commit();
            return $this->renderSuccess('操作成功', url('movies.video/index'));
        } catch (\Exception $e) {
            Db::rollback();
            return $this->renderError('操作失败');
        }
    }

    /**
     * 集列表
     * @param $video_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function gatherList($video_id = 0)
    {
        $list = Db::name('video_gather')->where('video_id', $video_id)->where('wxapp_id', $this->getWxappId())->select()->toArray();
        foreach ($list as &$value) {
            $value['grade'] = $value['grade_id'] ? Db::name('user_grade')->where('grade_id', $value['grade_id'])->where('wxapp_id', $this->getWxappId())->value('name') : '无限制';
            $value['video_name'] = Db::name('video')->where('video_id', $value['video_id'])->where('wxapp_id', $this->getWxappId())->value('video_name');
        }
        return $this->fetch('gatherList', compact('list'));
    }

    /**
     * 删除
     * @param $video_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($video_id)
    {
        // 门店详情
        $model = VideoModel::detail($video_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 删除集
     * @param $gather_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function deleteGather($gather_id)
    {
        $res = Db::name('video_gather')->where('gather_id', $gather_id)->delete();
        if (!$res) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 修改商品状态
     * @param $video_id
     * @param boolean $state
     * @return array
     */
    public function state($video_id, $state)
    {
        // 视频详情
        $model = VideoModel::detail($video_id);
        if (!$model->setStatus($state)) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

}